import bg.cookbook.Util;

import com.adobe.webapis.flickr.FlickrError;
import com.adobe.webapis.flickr.FlickrService;
import com.adobe.webapis.flickr.PagedPhotoList;
import com.adobe.webapis.flickr.Photo;
import com.adobe.webapis.flickr.User;
import com.adobe.webapis.flickr.events.FlickrResultEvent;

import flash.events.Event;
import flash.events.MouseEvent;

import mx.controls.Alert;
import mx.events.FlexEvent;

import spark.events.DropDownEvent;

[Bindable]
private var photosArray:Array;

[Bindable]
private var queriedUser:String = "OurGuy";

private var service:FlickrService;


[Bindable]
private var flickrId:String;

private var _v:Vector.<String>;
public function get listOfImages():Vector.<String> 
{
	return _v;
}
/**
 * Note: This API requires the userid/pw to be logged in thru the flickr website to access
 *          many methods, but some public methods are available just by username, such as below.
 */
private function onGet(event:Event):void
{
	if (userid.text!=null && userid.text.length >0)
		queriedUser=userid.text;
	if (queriedUser!=null && queriedUser.length >0) {
		service.addEventListener( FlickrResultEvent.PEOPLE_FIND_BY_USERNAME,onPeopleFindByUsername);
		service.people.findByUsername( queriedUser );
	}
	else Alert.show("Please enter a flickr userid.");
	
}


private function onPeopleFindByUsername(event:FlickrResultEvent):void
{
	var user:User = event.data.user;
	if (user != null) {
		flickrId = user.nsid;
		service.addEventListener(FlickrResultEvent.PEOPLE_GET_PUBLIC_PHOTOS, genericResponseHandler);
		service.people.getPublicPhotos(flickrId);
	}
	else {
		Alert.show("Flickr userid " + queriedUser + " not found.");
	}
}

/**
 * Generically handle the response to a flickr method call - just output
 * the information in the event to the screen.
 */
private function genericResponseHandler( event:FlickrResultEvent ):void 
{
	var v:Vector.<String>;
	if ( event.success ) {
		if (event.data.photos is PagedPhotoList) 
		{
			var list:PagedPhotoList = PagedPhotoList(event.data.photos);
			dgData.dataProvider = list.photos;
			//						trace(list.photos);
			v = new Vector.<String>();
			for each(var p:Photo in list.photos)
			{
				var photoUrl:String = Util.createUrl(p.server.toString(),p.id.toString(),p.secret,p.farmId.toString());
				v.push(photoUrl);
				
			}
//			im.listOfImages = v;
			_v = v;
		}
	} 
	else {
		// Encountered some kind of error on Flickr...
		var e:FlickrError = FlickrError( event.data.error );
		Alert.show("Flickr API error: " + e.errorMessage);
		
	}
}

protected function vgroup1_creationCompleteHandler(event:FlexEvent):void
{
	var api_key:String = "11480c59fcca529e932eb7d38a37dba6";
	service = new FlickrService( api_key );
	service.secret = "23a6d95d13a216c5";
	service.addEventListener( FlickrResultEvent.AUTH_GET_FROB, onAuthGetFrob );
	service.auth.getFrob();
}

private function onAuthGetFrob( event:FlickrResultEvent ):void {
	if (event.success) {
		var frob:String = String( event.data.frob );
		onGet(null);
	}
}

protected function dropdownlist1_closeHandler(event:DropDownEvent):void
{
	userid.text=usersArr[DDL.selectedIndex];
	if (userid.text!=null && userid.text.length >0)
		queriedUser=userid.text;
	if (queriedUser!=null && queriedUser.length >0) {
		service.addEventListener( FlickrResultEvent.PEOPLE_FIND_BY_USERNAME,onPeopleFindByUsername);
		service.people.findByUsername( queriedUser );
	}
	else Alert.show("Please enter a flickr userid.");
}

protected function hideOrShow(event:MouseEvent):void 
{
	//TODO: animate
	if(this.x>=0) 
	{
		//hide me
		this.x = -(btn.x + btn.width/2);
	}
	else
	{
		this.x = 0;
	}
}